package com.example.text6_server;



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.tv_main_content)
    TextView main_content;

    private Handler handler;
    private Unbinder unbinder;
    private UdpServer udpServer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        unbinder = ButterKnife.bind(this);

        handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == UdpServer.WHAT) {
                    main_content.append(((String) msg.obj) + "\n");
                }
            }
        };

        udpServer = new UdpServer(handler);
        udpServer.start();
    }

    @Override
    protected void onDestroy() {
        if (udpServer != null) {
            udpServer.onDestory();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }

        super.onDestroy();
    }
}